package blogsys.demo.controler;

import blogsys.demo.bean.UserBlog;
import blogsys.demo.server.UserBlogServer;
import blogsys.demo.utils.JsonResult;
import blogsys.demo.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;


/**
 * 博客信息控制
 */
@RestController
@RequestMapping("/blog")
public class UserBlogControler {

    @Autowired
    private UserBlogServer userBlogServer;
    /**
     * 获取所有的博客信息
     * @param pageNum 页码
     * @return
     */
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public JsonResult getAllBlog(@RequestParam(name = "pageNum",defaultValue = "1")int pageNum){
        return userBlogServer.getAllBolg(pageNum);
    }

    /**
     * 获取用户的博客
     * @param request
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/selectById",method = RequestMethod.GET)
    public JsonResult getBlogById(HttpServletRequest request,@RequestParam(name = "pageNum",defaultValue = "1")int pageNum){
        return userBlogServer.getBlogById(request,pageNum);
    }


    /**
     * 添加一条博客
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertById",method = RequestMethod.GET)
    public JsonResult addBlog(HttpServletRequest request, UserBlog userBlog){
        return userBlogServer.addBlogById(request,userBlog);
    }

    /**
     * 修改博客
     * @param userBlog
     * @return
     */
    @RequestMapping(value = "/updateBlog",method = RequestMethod.GET)
    public JsonResult updateBlog(UserBlog userBlog){
        System.out.println(userBlog.toString());
        return userBlogServer.updateBlog(userBlog);
    }

    /**
     * 删除博客
     * @param
     * @return
     */
    @RequestMapping(value = "/removerBlog",method = RequestMethod.GET)
    public JsonResult removerBlog(String  userBlogId){
        return userBlogServer.removeBlogById(userBlogId);
    }


}
